{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os, time, pickle, random, time\n",
    "from datetime import datetime\n",
    "import numpy as np\n",
    "from time import localtime, strftime\n",
    "import logging, scipy\n",
    "\n",
    "import tensorflow as tf\n",
    "import tensorlayer as tl\n",
    "from model import *\n",
    "from utils import *\n",
    "from config import config, log_config\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[!] samples/test exists ...\n",
      "  [TL] InputLayer  SRGAN_g/in: (?, 510, 339, 3)\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c: shape:[3, 3, 3, 64] strides:[1, 1, 1, 1] pad:SAME act:relu\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/0: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/0: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/0: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/0: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/0: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/1: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/1: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/1: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/1: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python2.7/site-packages/ipykernel_launcher.py:8: DeprecationWarning: `imread` is deprecated!\n",
      "`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n",
      "Use ``imageio.imread`` instead.\n",
      "  \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/1: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/2: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/2: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/2: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/2: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/2: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/3: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/3: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/3: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/3: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/3: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/4: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/4: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/4: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/4: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/4: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/5: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/5: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/5: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/5: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/5: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/6: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/6: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/6: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/6: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/6: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/7: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/7: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/7: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/7: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/7: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/8: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/8: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/8: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/8: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/8: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/9: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/9: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/9: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/9: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/9: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/10: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/10: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/10: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/10: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/10: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/11: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/11: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/11: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/11: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/11: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/12: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/12: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/12: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/12: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/12: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/13: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/13: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/13: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/13: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/13: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/14: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/14: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/14: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/14: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/14: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c1/15: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b1/15: decay:0.900000 epsilon:0.000010 act:relu is_train:False\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c2/15: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b2/15: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/b_residual_add/15: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n64s1/c/m: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] BatchNormLayer SRGAN_g/n64s1/b/m: decay:0.900000 epsilon:0.000010 act:identity is_train:False\n",
      "  [TL] ElementwiseLayer SRGAN_g/add3: size:(?, 510, 339, 64) fn:add\n",
      "  [TL] Conv2dLayer SRGAN_g/n256s1/1: shape:[3, 3, 64, 256] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] SubpixelConv2d  pixelshufflerx2/1: scale: 2 n_out_channel: 64 act: relu\n",
      "  [TL] Conv2dLayer SRGAN_g/n256s1/2: shape:[3, 3, 64, 256] strides:[1, 1, 1, 1] pad:SAME act:identity\n",
      "  [TL] SubpixelConv2d  pixelshufflerx2/2: scale: 2 n_out_channel: 64 act: relu\n",
      "  [TL] Conv2dLayer SRGAN_g/out: shape:[1, 1, 64, 3] strides:[1, 1, 1, 1] pad:SAME act:tanh\n",
      "[*] Load checkpoint/g_srgan.npz SUCCESS!\n",
      "took: 1.5281s\n",
      "LR size: (510, 339, 3) /  generated HR size: (1, 2040, 1356, 3)\n",
      "[*] save images\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python2.7/site-packages/ipykernel_launcher.py:32: DeprecationWarning: `imresize` is deprecated!\n",
      "`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n",
      "Use ``skimage.transform.resize`` instead.\n"
     ]
    }
   ],
   "source": [
    "image_path = './input_image/0124x4.png'\n",
    "\n",
    "save_dir = \"samples/{}\".format('test')\n",
    "tl.files.exists_or_mkdir(save_dir)\n",
    "checkpoint_dir = \"checkpoint\"\n",
    "\n",
    "###====================== PRE-LOAD DATA ===========================###\n",
    "valid_lr_img = scipy.misc.imread(image_path, mode='RGB')  # if you want to test your own image\n",
    "valid_lr_img = (valid_lr_img / 127.5) - 1   # rescale to ［－1, 1]\n",
    "\n",
    "\n",
    "size = valid_lr_img.shape\n",
    "t_image = tf.placeholder('float32', [None, size[0], size[1], size[2]], name='input_image')\n",
    "\n",
    "net_g = SRGAN_g(t_image, is_train=False, reuse=False)\n",
    "\n",
    "###========================== RESTORE G =============================###\n",
    "sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False))\n",
    "tl.layers.initialize_global_variables(sess)\n",
    "tl.files.load_and_assign_npz(sess=sess, name=checkpoint_dir+'/g_srgan.npz', network=net_g)\n",
    "\n",
    "###======================= EVALUATION =============================###\n",
    "start_time = time.time()\n",
    "out = sess.run(net_g.outputs, {t_image: [valid_lr_img]})\n",
    "print(\"took: %4.4fs\" % (time.time() - start_time))\n",
    "\n",
    "print(\"LR size: %s /  generated HR size: %s\" % (size, out.shape)) # LR size: (339, 510, 3) /  gen HR size: (1, 1356, 2040, 3)\n",
    "print(\"[*] save images\")\n",
    "tl.vis.save_image(out[0], save_dir+'/valid_gen.png')\n",
    "tl.vis.save_image(valid_lr_img, save_dir+'/valid_lr.png')\n",
    "\n",
    "out_bicu = scipy.misc.imresize(valid_lr_img, [size[0]*4, size[1]*4], interp='bicubic', mode=None)\n",
    "tl.vis.save_image(out_bicu, save_dir+'/valid_bicubic.png')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
